package ast.nodes;

import scanner.Token;
import ast.ASTNode;
import ast.Evaluation;

public class ProgramNode extends ASTNode {

    public ProgramNode()
    {
        super();
    }
    
    public ProgramNode(Token data)
    {
        super(data);
    }

    public String toString(int depth) {

        String childrenString = "";
        for (ASTNode child: getChildren()) {
            childrenString += child.toString(depth+1);
        }

        return indent(depth) + "Program:" + childrenString;
    }

    @Override
    public void run(Evaluation input) throws Exception {
        // run the begin node
        for(ASTNode child : children)
            if (child instanceof BeginNode)
                child.run(input);

        // run other nodes - note that there is probably only one series node
        // that's going to be run for each line
        do
        {
	        for(ASTNode child : children)
	            if(!(child instanceof BeginNode || child instanceof EndNode))
	                child.run(input);
        }
        while(input.nextLine());
        
        // run the end node
        for(ASTNode child : children)
            if (child instanceof EndNode)
                child.run(input);
    }
}
